<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Brand_m extends MY_Model
{
    const TBL = 'brand';

    function __construct()
    {
        parent::__construct();
    }

    /**
     * @param $id
     * @return mixed
     * 查询单条
     */
    public function get_brand_by_id($id)
    {
        $query = $this->db
            ->select('*')
            ->get_where('brand', ['id' => $id]);
        return $query->row_array();
    }

    /**
     * @param $id
     * @param $data
     * @return bool
     * 修改品牌信息
     */
    public function sava_brand($id, $data)
    {

        $this->db->where('id', $id);
        $this->db->update('brand', $data);
        return ($this->db->affected_rows() > 0) ? true : false;
    }

    /**
     * @param $data
     * @return bool
     * 新增品牌
     */
    public function add_brand($data)
    {

        $this->db->insert('brand', $data);
        if ($this->db->affected_rows() > 0) {
            return $this->db->insert_id();
        } else {
            return false;
        }
    }

    /**
     * @return mixed
     * 查询所有品牌
     */
    public function get_all_brand()
    {
        $this->db->select('*');
        $this->db->from('brand');
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            return $query->result_array();
        }
    }

    /**
     * 根据条件查询记录
     */
    public function get_brand_by($mobile, $yzm, $checked)
    {
        $map   = "mobile = '" . $mobile . "' AND yzm = '" . $yzm . "' AND is_checked = $checked";
        $sql   = "SELECT * FROM {$this->db->dbprefix}brand  where $map";
        $query = $this->db->query($sql);
        return $query->row_array();
    }

    /**
     * 根据条件查询记录
     */
    public function get_brand_by_type($mobile, $type)
    {
        $map   = "mobile = '" . $mobile . "' AND type = '" . $type . "'";
        $sql   = "SELECT * FROM {$this->db->dbprefix}brand  where $map";
        $query = $this->db->query($sql);
        return $query->row_array();
    }

    /**
     * @param $where
     * @return bool
     * 删除品牌
     */
    public function delete_brand_by($where)
    {
        $this->db->where($where)->delete('brand');
        return ($this->db->affected_rows() > 0) ? true : false;
    }

    public function getCarBrand()
    {
        $this->db->select('brand.cat_id as brand_id,brand.title as brand_title');
        $this->db->from('brand');
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            return $query->result_array();
        }
    }

    public function getCarSeries($brand_id)
    {
        $this->db->select('series.cat_id as series_id,series.title as series_title');
        $this->db->from('series');
        $this->db->where('series.brand_id', $brand_id);
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            return $query->result_array();
        }
    }
}
